package demo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * 字符集设置的filter，用以设置请求的编码
 * 
 * @author Administrator
 * 2012-9-2下午2:37:59
 */
public class EncodingFilter implements Filter {

	private  String targetEncoding = "ASCII";
	
	@Override
	public void init(FilterConfig config) throws ServletException {
	    
		this.targetEncoding = config.getInitParameter("encoding");
		System.out.println("===> EncodingFilter 初始化成功！1");
	}

	@Override
	public void doFilter(ServletRequest srequest, ServletResponse sresponse,
			FilterChain chain) throws IOException, ServletException {
	    
	    System.out.println("===> encodingFilter 被调用 ");
	    
		HttpServletRequest request = (HttpServletRequest)srequest; 
		
		request.setCharacterEncoding(targetEncoding); 
		
		chain.doFilter(srequest,sresponse); 
	}
	
	@Override
	public void destroy() {
	    System.out.println("===> EncodingFilter 销毁！1");
	}
}
